<!--全局顶部栏-->
<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <RouterLink to="/">
          <div class="title-bar">
            <img class="logo" src="../assets/logo.png" alt = "logo" />
            <div class="title">鱼皮皮云图库</div>
          </div>
        </RouterLink>
      </a-col>

      <a-col flex="auto">
        <a-menu v-model:selectedKeys="current" mode="horizontal" :items="items" @click="doMenuClick"/>
      </a-col>
      <!--用户信息展示栏-->
      <a-col flex="120px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.id">
            <a-dropdown>
              <a-space>
                <a-avatar :src="loginUserStore.loginUser.userAvatar"/>
                {{ loginUserStore.loginUser.userName ?? '无名' }}
              </a-space>
              <template #overlay>
                <a-menu>
                  <a-menu-item @click="doLogout">
                    <a href="javascript:;">
                      <LogoutOutlined />
                      退出登录
                    </a>
                  </a-menu-item>
                </a-menu>
              </template>
            </a-dropdown>

          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>



  </div>
</template>
<script lang="ts" setup>
import {computed, h, ref} from 'vue';
import { HomeOutlined ,LogoutOutlined} from '@ant-design/icons-vue';
import {MenuProps, message} from 'ant-design-vue';
import { useRouter } from "vue-router";
import {useLoginUserStore} from "@/stores/useLoginUserStore";
import {userLogoutUsingPost} from "@/api/userController";
const loginUserStore = useLoginUserStore();

//菜单列表
const originItems = [
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: "主页",
    title: "主页",
  },
  {
    key: '/add_picture',
    label: "创建图片",
    title: "创建图片",
  },
  {
    key: '/admin/userManage',
    label: "用户管理",
    title: "用户管理",
  },
  {
    key: '/admin/pictureManage',
    label: "图片管理",
    title: "图片管理",
  },
]

const router = useRouter();
//路由跳转事件
const doMenuClick = ({ key }) =>{
  router.push({
    path:key
  })
}
//当前高亮的顶部菜单项
const current = ref<string[]>([]);
//监听路由变化，更新高亮菜单项
router.afterEach((to,from,next)=>{
  current.value = [to.path]
})

//退出登录/用户注销
const doLogout = async () => {
  const res = await userLogoutUsingPost()
  if (res.data.code === 0){
    //清空登录用户信息
    loginUserStore.setLoginUser({
      userName:'未登录',
    })
    message.success('退出登录成功')
    //返回登录页面
    router.push({
      path:'/user/login'
    })
  }else {
    message.error('退出登录失败，' + res.data.message)
  }
}

//过滤菜单项,地址以 /admin 开头的页面都会限制为仅管理员可见和可用
const filterMenus = (menus = [] as MenuProps['items'])=>{
  return menus?.filter((menu)=>{
    if (menu.key.startsWith('/admin')){
      const loginUser = loginUserStore.loginUser;
      if (!loginUser || loginUser.userRole !== 'admin'){
        return false;
      }
    }
    return true;
  })
}

//展示在菜单的路由数组
const items = computed(()=> filterMenus(originItems))

</script>

<style scoped>
#globalHeader .title-bar {
  display: flex;
  align-items: center;
}

#globalHeader .title {
  color: black;
  font-size: 18px;
  margin-left: 16px;
}

#globalHeader .logo {
  height: 48px;
}
</style>








